/***************************************************************************
*                                                                         *
*   This file is part of the Fotowall project,                            *
*       http://code.google.com/p/fotowall                                 *
*                                                                         *
*   Copyright (C) 2009 by Enrico Ros <enrico.ros@gmail.com>               *
*                                                                         *
*   This program is free software; you can redistribute it and/or modify  *
*   it under the terms of the GNU General Public License as published by  *
*   the Free Software Foundation; either version 2 of the License, or     *
*   (at your option) any later version.                                   *
*                                                                         *
* ----------------------------------------------------------------------- *
*																		   *
*	Modifications for FotoKinect, Spring 2011 by Gianna Badiali,		   *
*                                                   Gabriel Fernandez,	   *
*                                                   Kelsey Tripp, and     *
*                                                   Sam Swarr             *
*																		   *
***************************************************************************/

#include "AbstractConfig.h"
#include "AbstractContent.h"
#include "Desk.h"
#include "RenderOpts.h"
#include "StyledButtonItem.h"
#include "ui_AbstractConfig.h"
#include "frames/FrameFactory.h"
#include <QGraphicsSceneMouseEvent>
#include <QFileDialog>
#include <QListWidgetItem>
#include <QPainter>
#include <QPixmapCache>
#include <QPushButton>
#include <QStyle>
#include <QWidget>
#include <QSettings>

#if QT_VERSION >= 0x040600
#include <QPropertyAnimation>
#endif

AbstractConfig::AbstractConfig(AbstractContent * content, QGraphicsItem * parent)
	: QGraphicsProxyWidget(parent)
	, m_content(content)
	, m_commonUi(new Ui::AbstractConfig())
	, m_closeButton(0)
	, m_okButton(0)
{
	// close button
	m_closeButton = new StyledButtonItem(tr(" x "), font(), this);//this, ":/data/button-close.png", ":/data/button-close-hovered.png", ":/data/button-close-pressed.png");
	connect(m_closeButton, SIGNAL(clicked()), this, SLOT(slotRequestClose()));

	// WIDGET setup
	QWidget * widget = new QWidget();
#if QT_VERSION < 0x040500
	widget->setAttribute(Qt::WA_NoSystemBackground, true);
#else
	widget->setAttribute(Qt::WA_TranslucentBackground, true);
#endif
	m_commonUi->setupUi(widget);

	connect(m_commonUi->front, SIGNAL(clicked()), m_content, SLOT(slotStackFront()));
	connect(m_commonUi->raise, SIGNAL(clicked()), m_content, SLOT(slotStackRaise()));
	connect(m_commonUi->lower, SIGNAL(clicked()), m_content, SLOT(slotStackLower()));
	connect(m_commonUi->back, SIGNAL(clicked()), m_content, SLOT(slotStackBack()));

	// ITEM setup
	setWidget(widget);
	static qreal s_propZBase = 99999;
	setZValue(s_propZBase++);

#if QT_VERSION >= 0x040600
	// fade in animation
	QPropertyAnimation * ani = new QPropertyAnimation(this, "opacity");
	ani->setEasingCurve(QEasingCurve::OutCubic);
	ani->setDuration(400);
	ani->setStartValue(0.0);
	ani->setEndValue(1.0);
	ani->start(QPropertyAnimation::DeleteWhenStopped);
#endif
}

AbstractConfig::~AbstractConfig()
{
	delete m_frame;
	delete m_commonUi;
}

void AbstractConfig::dispose()
{
#if QT_VERSION >= 0x040600
	// fade out animation, then delete
	QPropertyAnimation * ani = new QPropertyAnimation(this, "opacity");
	connect(ani, SIGNAL(finished()), this, SLOT(deleteLater()));
	ani->setEasingCurve(QEasingCurve::OutCubic);
	ani->setDuration(200);
	ani->setEndValue(0.0);
	ani->start(QPropertyAnimation::DeleteWhenStopped);
#else
	// delete this now
	deleteLater();
#endif
}

AbstractContent * AbstractConfig::content() const
{
	return m_content;
}

void AbstractConfig::keepInBoundaries(const QRect & rect)
{
	QRect r = mapToScene(boundingRect()).boundingRect().toRect();
	r.setLeft(qBound(rect.left(), r.left(), rect.right() - r.width()));
	r.setTop(qBound(rect.top(), r.top(), rect.bottom() - r.height()));
	setPos(r.topLeft());
}


void AbstractConfig::showOkButton(bool show)
{
	if (show) {
		if (!m_okButton) {
			m_okButton = new StyledButtonItem(tr("ok"), font(), this);
			connect(m_okButton, SIGNAL(clicked()), this, SLOT(slotOkClicked()));
			layoutButtons();
		}
		m_okButton->show();
	} else if (m_okButton)
		m_okButton->hide();
}

void AbstractConfig::mousePressEvent(QGraphicsSceneMouseEvent * event)
{
	QGraphicsProxyWidget::mousePressEvent(event);
	if (!event->isAccepted() && event->button() == Qt::RightButton)
		slotRequestClose();
	event->accept();
}

void AbstractConfig::mouseDoubleClickEvent(QGraphicsSceneMouseEvent * event)
{
	QGraphicsProxyWidget::mouseDoubleClickEvent(event);
	event->accept();
}

void AbstractConfig::paint(QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget)
{
	// draw custom background
	painter->save();
	m_frame->paint(painter, boundingRect().toRect(), false, false);
	painter->restore();

	// unbreak parent
	QGraphicsProxyWidget::paint(painter, option, widget);
}

void AbstractConfig::resizeEvent(QGraphicsSceneResizeEvent * event)
{
	layoutButtons();
	QGraphicsProxyWidget::resizeEvent(event);
}

void AbstractConfig::slotRequestClose()
{
	Desk * desk = static_cast<Desk *>(scene());
	desk->slotDeleteConfig(this);
}


void AbstractConfig::layoutButtons()
{
	// layout the close button
	QRect cRect = boundingRect().toRect().adjusted(12, 12, -12, -12);
	if (QApplication::isLeftToRight()) {
		m_closeButton->setPos(cRect.right() - m_closeButton->boundingRect().width(), cRect.top());
		if (m_okButton)
			m_okButton->setPos(m_closeButton->pos().x() - m_okButton->boundingRect().width() - 8, cRect.top());
	} else {
		m_closeButton->setPos(cRect.left(), cRect.top());
		if (m_okButton)
			m_okButton->setPos(cRect.left() + m_closeButton->boundingRect().width() + 8, cRect.top());
	}
}
